<?php
/**
 * Created by PhpStorm.
 * User: wuzt
 * Date: 14-12-26
 * Time: 下午3:12
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Thumb extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('ftp');
        $this->load->helper('image');
        $this->config->load('server');
    }

    public function cut() {
        $ftp_config = $this->config->item('ftp');
        $dir = BASEPATH . '../www/api/data/thumb/';
        $path_info = explode('/', $_SERVER['REQUEST_URI']);
        $folder = $path_info[3].'/';
        $filename = $path_info[4];
        $img_info = explode('.', $filename);
        $file_info = explode('_', $img_info[0]);
        $ext = '.' . $img_info[1];
        $local_path = BASEPATH . '../www/api/data/temp/' . $file_info[0].$ext;
        $ftp_path = $ftp_config['attch_basepath'].$folder.$file_info[0].$ext;
        #1.FTP get img
        $config['hostname'] = $ftp_config['host'];
        $config['username'] = $ftp_config['username'];
        $config['password'] = $ftp_config['password'];
        $config['debug'] = TRUE;
        $this->ftp->connect($config);
        if(!$this->ftp->download($ftp_path, $local_path)) {
            output_img(false);
        }
        $this->ftp->close();
        #2.创建目录
        if(!is_dir($dir.$folder)) {
            mkdir($dir.$folder);
        }
        #3.resize img
        $temp_image = $dir.$folder.'temp_'.$file_info[0].'_'.$file_info[1].'_'.$file_info[2].$ext;
        if(!image_resize($local_path, $file_info[1], $file_info[2], $temp_image)) {
            output_img(false);
        }
        #4.crop
        $new_image = $dir . '../../' . $_SERVER['REQUEST_URI'];
        if(!image_crop($temp_image, $file_info[1], $file_info[2], $new_image)) {
            output_img(false);
        }
        #5.删除文件
        @unlink($local_path);
        @unlink($temp_image);
        output_img($new_image);
    }

}